//
//  StockGoodsRankView.m
//  quxiangtao
//
//  Created by xbmios on 2018/7/26.
//  Copyright © 2018年 shanhaijin. All rights reserved.
//

#import "StockGoodsRankView.h"
#import "StockGoodsRankContentView.h"

@interface StockGoodsRankView ()<UIScrollViewDelegate>

@property (strong, nonatomic) UIScrollView *titleScrollView;
@property (strong, nonatomic) UIScrollView *contentScrollView;

@property (strong, nonatomic) UIView *sliderView;

@property (strong, nonatomic) NSMutableArray *lablexArr;

@end


@implementation StockGoodsRankView

- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        
        [self addSubview:self.titleScrollView];
        [self addSubview:self.contentScrollView];
        
        [self createUI];
        
        [self setupTitleScrollView];
        
        [self setupContentScrollView];
    }
    
    return self;
}

#pragma mark -标题点击
- (void)labelClick:(UIGestureRecognizer *)recognizer {
    
    UILabel *label = (UILabel *)recognizer.view;
    
    [self.contentScrollView setContentOffset:CGPointMake(WIDTH * label.tag, 0) animated:YES];
    
    for (int i = 0; i < self.lablexArr.count; i ++) {
        
        UILabel *temp = self.titleScrollView.subviews[i];
        
        temp.font = Font(30);
        temp.textColor = Color999999;
    }
    
    label.font = [UIFont fontWithName:@"Helvetica-Bold" size:jisuan(30)];
    label.textColor = Color333333;
    
    CGFloat width = self.titleScrollView.frame.size.width;
    CGFloat offsetX = label.center.x - width * 0.5;
    // 最大偏移量
    CGFloat maxOffsetx = self.titleScrollView.contentSize.width - width;
    
    if (offsetX < 0) {
        
        offsetX = 0;
        
    } else if (offsetX > maxOffsetx) {
        
        offsetX = maxOffsetx;
        
    }
    
    [self.titleScrollView setContentOffset:CGPointMake(offsetX, 0) animated:NO];
    
    CGFloat x = [self.lablexArr[label.tag] floatValue];
    
    __weak typeof(self) weakSelf = self;
    [UIView animateWithDuration:0.3 animations:^{
        CGRect rect = weakSelf.sliderView.frame;
        rect.origin.x = x;
        weakSelf.sliderView.frame = rect;
    }];
    
}

#pragma mark -scrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    
    [self scrollViewDidEndScrollingAnimation:scrollView];
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
    
    NSInteger index = scrollView.contentOffset.x / WIDTH;
    
    StockGoodsRankContentView *contentView = self.contentScrollView.subviews[index];
    UILabel *label = self.titleScrollView.subviews[index];
    
    for (int i = 0; i < self.lablexArr.count;  i ++) {
        
        UILabel *temp = self.titleScrollView.subviews[i];
        temp.font = Font(30);
        temp.textColor = Color999999;
    }
    
    label.font = [UIFont fontWithName:@"Helvetica-Bold" size:jisuan(30)];
    label.textColor = Color333333;
    
    CGFloat width = self.titleScrollView.frame.size.width;
    CGFloat offsetX = label.center.x - width * 0.5;
    // 最大偏移量
    CGFloat maxOffsetx = self.titleScrollView.contentSize.width - width;
    if (offsetX < 0) {
        offsetX = 0;
    } else if (offsetX > maxOffsetx) {
        offsetX = maxOffsetx;
    }
    
    [self.titleScrollView setContentOffset:CGPointMake(offsetX, 0) animated:NO];
    
    // 添加
    CGFloat vcW = scrollView.frame.size.width;
    CGFloat vcH = scrollView.frame.size.height;
    CGFloat vcY = 0;
    CGFloat vcX = index * vcW;
    contentView.frame = CGRectMake(vcX, vcY, vcW, vcH);
    
}

- (void)createUI {
    
    __weak typeof(self) weakSelf = self;
    
    [self.titleScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.mas_left);
        make.right.mas_equalTo(weakSelf.mas_right);
        make.top.mas_equalTo(weakSelf.mas_top);
        make.height.mas_equalTo(jisuan(100));
        
    }];
    [self.contentScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.mas_left);
        make.right.mas_equalTo(weakSelf.mas_right);
        make.bottom.mas_equalTo(weakSelf.mas_bottom);
        make.top.mas_equalTo(weakSelf.titleScrollView.mas_bottom);
        
    }];
    
}

- (void)setupContentScrollView {
    
    for (int i = 0; i < self.lablexArr.count; i ++) {
        
        StockGoodsRankContentView *contentView = [[StockGoodsRankContentView alloc] init];
        contentView.type = i;

        [self.contentScrollView addSubview:contentView];
        
    }
    
    StockGoodsRankContentView *tempView = [self.contentScrollView.subviews firstObject];
    tempView.frame = CGRectMake(0, 0, WIDTH, jisuan(834));
    
    self.contentScrollView.contentSize = CGSizeMake(WIDTH * self.contentScrollView.subviews.count, 0);
    
}

- (void)setupTitleScrollView {
    
    NSArray *array = @[@"最强王者", @"最快下手"];
    self.lablexArr = [NSMutableArray array];
    CGFloat x = WIDTH / 4.0 - jisuan(75);
    
    CGFloat width = WIDTH / 2.0;
    
    for (int i = 0 ; i < array.count; i ++) {
        
        UILabel *label = [[UILabel alloc] init];
        label.userInteractionEnabled = YES;
        label.tag = i;
        label.text = array[i];
        //        label.font = Font(30);
        label.textAlignment = NSTextAlignmentCenter;
        [label sizeToFit];
        
        if (i == 0) {
            
            label.textColor = Color333333;
            label.font = [UIFont fontWithName:@"Helvetica-Bold" size:jisuan(30)];
            
        }else {
            
            label.textColor = Color999999;
            label.font = Font(30);
        }
        
        label.frame = CGRectMake(width * i, 0, width, jisuan(100));
        
        CGFloat sliderX = x + width * i;
        [self.lablexArr addObject:[NSString stringWithFormat:@"%.2f", sliderX]];
        
        [self.titleScrollView addSubview:label];
        
        UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelClick:)];
        [label addGestureRecognizer:recognizer];
    }
    
    
    self.sliderView.frame = CGRectMake(x, jisuan(90), jisuan(150), jisuan(4));
    [self.titleScrollView addSubview:self.sliderView];
    
    self.titleScrollView.contentSize = CGSizeMake(WIDTH, 0);
    
}


#pragma mark -lazy
- (UIScrollView *)titleScrollView {
    
    if (!_titleScrollView) {
        
        _titleScrollView = [[UIScrollView alloc] init];
        _titleScrollView.backgroundColor = WhiteColor;
        
        _titleScrollView.showsVerticalScrollIndicator = NO;
        _titleScrollView.showsHorizontalScrollIndicator = NO;
    }
    
    return _titleScrollView;
}
- (UIScrollView *)contentScrollView {
    
    if (!_contentScrollView) {
        
        _contentScrollView = [[UIScrollView alloc] init];
        _contentScrollView.backgroundColor = WhiteColor;
        
        _contentScrollView.showsVerticalScrollIndicator = NO;
        _contentScrollView.showsHorizontalScrollIndicator = NO;
        
        _contentScrollView.delegate = self;
        _contentScrollView.scrollEnabled = NO;
        
    }
    
    return _contentScrollView;
}

- (UIView *)sliderView {
    
    if (!_sliderView) {
        
        _sliderView = [[UIView alloc] init];
        _sliderView.backgroundColor = Colorff0000;
    }
    return _sliderView;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
